package org.jeecg.modules.wangge;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.util.JSONUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.GPSUtil;
import org.jeecg.modules.pucheng.bo.RoadDataBo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.NumberFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/datapull/luz")
@Api(tags = "路长数据同步")
public class LuZhangDataController {

    //@ApiOperation("导入")
    @RequestMapping("/importData")
    public Result<?> ftl() {
        String filePath = "E:\\征信云\\pucheng\\蒲城县路段管养信息(1).xls";
        List<RoadDataBo> list = new ArrayList<>();
        try ( InputStream in = new FileInputStream(filePath)){
            HSSFWorkbook wb = new HSSFWorkbook(in);
            HSSFSheet sheet = wb.getSheetAt(0);

            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                HSSFRow row = sheet.getRow(rowNum);
                if (row == null) {
                    continue;
                }
                RoadDataBo dataBo = new RoadDataBo();
                dataBo.setAdminUserId(getValue(row.getCell(0)));
                dataBo.setRoadPathCode(getValue(row.getCell(1)));
                dataBo.setRoadPathName(getValue(row.getCell(2)));
                dataBo.setRoadSegmentCode(getValue(row.getCell(3)));
                dataBo.setTownCode(getValue(row.getCell(4)));
                dataBo.setTown(getValue(row.getCell(5)));
                dataBo.setRoadStart(getValue(row.getCell(6)));
                dataBo.setRoadEnd(getValue(row.getCell(7)));


                dataBo.setTechGrade(getValue(row.getCell(8)));
                dataBo.setLayerType(getValue(row.getCell(9)));
                dataBo.setRoadBaseWidth(getValue(row.getCell(10)));
                dataBo.setRoadLayerWidth(getValue(row.getCell(11)));
                dataBo.setRoadLayerThick(getValue(row.getCell(12)));
                dataBo.setDesignSpeed(getValue(row.getCell(13)));
                dataBo.setTotalLenth(getValue(row.getCell(14)));
                dataBo.setUnitType(getValue(row.getCell(15)));

                dataBo.setConservationType(getValue(row.getCell(16)));
                dataBo.setMapAxis(getValue(row.getCell(17)));
                list.add(dataBo);

            }
        } catch (Exception e) {

        }
        return Result.ok(list);
    }

    private static String getValue(HSSFCell hssfCell) {
        String value = "";
        if (hssfCell == null) {
            return null;
        }
        if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
            // 返回布尔类型的值
            return String.valueOf("");
        } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            // 返回数值类型的值
            NumberFormat format = NumberFormat.getInstance();
            format.setMinimumFractionDigits(0);// 设置数值的小数部分允许的最小位数。
            format.setMaximumFractionDigits(9);// 设置数值的小数部分允许的最大位数。
            value = format.format(hssfCell.getNumericCellValue());// 将末尾的0去掉。使单元格内容“1”，读取到的“1.00”变成“1”
            value = value.replace(",", "");
        } else if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            //|| hssfCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {// XSSFCell与HSSFCell值相同
            value = hssfCell.getStringCellValue();
        }
        return value.trim();
    }


    @ApiOperation("getXYC")
    @GetMapping("/getXYC")
    public Result<?> getXYC(){
        String json = readJson("classpath:org/jeecg/modules/wangge/json/listDataA.json");
        JSONArray array = JSON.parseArray(json);
        List<RoadDataBo> roadDataBos = array.toJavaList(RoadDataBo.class);
        int x = 0;
        int c = 0;
        int y = 0;
        Set<String> setX  = new HashSet<>();
        Set<String> setY = new HashSet<>();
        Set<String> setC  = new HashSet<>();
        for (RoadDataBo bo:roadDataBos) {
            if (StringUtils.isNotBlank(bo.getRoadPathCode())){
                if ( bo.getRoadPathCode().startsWith("Y")){
                    if (setY.contains(bo.getRoadPathCode())){
                        continue;
                    }
                    y += 1;
                    setY.add(bo.getRoadPathCode());
                } else if( bo.getRoadPathCode().startsWith("X")){
                    if (setX.contains(bo.getRoadPathCode())){
                        continue;
                    }
                    x += 1;
                    setX.add(bo.getRoadPathCode());
                } else if( bo.getRoadPathCode().startsWith("C")){
                    if (setC.contains(bo.getRoadPathCode())){
                        continue;
                    }
                    c += 1;
                    setC.add(bo.getRoadPathCode());
                }
            }
        }
        Map<String,Integer> map = new HashMap<String,Integer>();
        map.put("X",x);
        map.put("C",c);
        map.put("Y",y);
        return Result.ok(map);
    }


    @ApiOperation("getWG4")
    @GetMapping("/getWG4")
    public Result<?> getWG4(){
        String json = readJson("classpath:org/jeecg/modules/wangge/json/listDataA.json");
        JSONArray array = JSON.parseArray(json);
        List<RoadDataBo> roadDataBos = array.toJavaList(RoadDataBo.class);
        for (RoadDataBo bo:roadDataBos) {
            String mapAxis = bo.getMapAxis();
            if(StringUtils.isNotBlank(mapAxis)){
                String[] arr = mapAxis.split(";");
                String res = "";
                for(String s : arr) {
                    String[] lntlat = s.split(",");
                    Double lnt = Double.parseDouble(lntlat[0]);
                    Double lat = Double.parseDouble(lntlat[1]);

                    double[] bd09 = GPSUtil.gcj02_To_Gps84(lnt, lat);
                    res += bd09[0] + "," + bd09[1] + ";";
                }
                res = res.substring(0, res.lastIndexOf(";"));
                bo.setMapAxis(res);
            } else {
                bo.setMapAxis(null);
            }
        }
        return Result.ok(roadDataBos);
    }


    private String readJson(String jsonSrc) {
        String json = "";
        try {
            //File jsonFile = ResourceUtils.getFile(jsonSrc);
            //json = FileUtils.re.readFileToString(jsonFile);
            //换个写法，解决springboot读取jar包中文件的问题
            InputStream stream = getClass().getClassLoader().getResourceAsStream(jsonSrc.replace("classpath:", ""));
            json = IOUtils.toString(stream);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
        return json;
    }
}
